﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.Reflection;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data;

namespace Baubixinh_client
{
    public partial class frmConnectionSetting : DevExpress.XtraEditors.XtraForm
    {
        public bool SettingSaved { get; set; }

        public frmConnectionSetting()
        {
            InitializeComponent();
        }

        private void frmConnectionSetting_Load(object sender, EventArgs e)
        {
            xInit();
        }

        private bool xInit()
        {
            bool blnResult = false;
            try
            {
                SqlConnectionStringBuilder objSqlConn = new SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["Connection String"].ToString());

                txtServer.Text = clsCommon.fncCnvNullToString(objSqlConn["Data Source"]);
                txtDbName.Text = clsCommon.fncCnvNullToString(objSqlConn["Initial Catalog"]);
                txtUsername.Text = clsCommon.fncCnvNullToString(objSqlConn["User ID"]);
                txtPassword.Text = clsCommon.fncCnvNullToString(objSqlConn["Password"]);

                txtServer.Focus();
                this.SettingSaved = false;

                return true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            return blnResult;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                SqlConnectionStringBuilder objSqlConn = new SqlConnectionStringBuilder(System.Configuration.ConfigurationManager.ConnectionStrings["Connection String"].ToString());

                objSqlConn["Data Source"] = txtServer.Text.Trim();
                objSqlConn["Initial Catalog"] = txtDbName.Text.Trim();
                objSqlConn["User ID"] = txtUsername.Text.Trim();
                objSqlConn["Password"] = txtPassword.Text.Trim();

                Program.gobjConn.Close();
                //Program.gobjDb = EnterpriseLibraryContainer.Current.GetInstance<Database>("Connection String");
                //Program.gobjConn = Program.gobjDb.CreateConnection();
                Program.gobjConn.ConnectionString = objSqlConn.ConnectionString;

                try
                {
                    Program.gobjConn.Open();
                }
                catch
                {
                    clsCommon.fncMsgError("Không thể kết nối tới cơ sở dữ liệu. Xin hãy kiểm tra lại");
                    txtServer.Focus();
                    return;
                }

                System.Configuration.Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(System.Configuration.ConfigurationUserLevel.None);
                config.ConnectionStrings.ConnectionStrings["Connection String"].ConnectionString = objSqlConn.ConnectionString;
                config.Save(System.Configuration.ConfigurationSaveMode.Modified, true);
                System.Configuration.ConfigurationManager.RefreshSection("connectionStrings");

                this.Close();
                this.SettingSaved = true;
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
            finally { Cursor.Current = Cursors.Default; }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception ex) { clsCommon.fncMsgError(MethodBase.GetCurrentMethod().ReflectedType.Name, MethodBase.GetCurrentMethod().Name, ex.Message); }
        }
    }
}